package w83b.w83bOperacionesPaso.w83bActas;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import w83b.w83bUtilidadesComunes.W83bClsConstantes;

import es.ejie.frmk.presentacion.operacion.paso.Q70ActionPasoOP;
import es.ejie.frmk.presentacion.sesiondatos.Q70ContextoEJIE;
public class W83bAdjuntarArchivoOPAction extends Q70ActionPasoOP{

	
	protected void processOP(Q70ContextoEJIE contexto, ActionErrors errores, ActionMapping mapping,
			ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
	}

	protected ActionForward processForward(Q70ContextoEJIE contexto, ActionErrors errors, ActionMapping mapping, 
			ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		String forward="ok";
		String action = request.getParameter("action");
		HashMap datos=null;
		ArrayList datosBorrado=null;
		ArrayList datosBorradoCompleto=null;
		if (StringUtils.isNotEmpty(action) && "delete".equals(action))//borramos docs que acabamos de insertar
		{
			String indice=request.getParameter("indice");
			
			if (contexto.get("w83bMapaFileActas")!= null)
			{
				datos=(HashMap)contexto.get("w83bMapaFileActas");
				datos.remove(indice);
			}
			
		}
		else{
			if (StringUtils.isNotEmpty(action) && "borradoFisico".equals(action))//borramos docs que ya existian
			{
				String indice=request.getParameter("indice");
				if (contexto.get("w83bListaBorradoFileActas")== null)
				{
					datosBorrado=new ArrayList();
					datosBorradoCompleto=new ArrayList();
					contexto.set("w83bListaBorradoFileActas", datos);
				}
				else
				{
					datosBorrado=(ArrayList)contexto.get("w83bListaBorradoFileActas");
					datosBorradoCompleto=(ArrayList)contexto.get("w83bListaBorradoFileActasCompleto");
				}
				//
				datos=(HashMap)contexto.get("w83bMapaFileActas");
				datos.remove(indice);//
				String idDocumento=request.getParameter("idDocumento");
				String textDocumento=request.getParameter("textDocumento");
				datosBorrado.add(idDocumento);
				datosBorradoCompleto.add(new StringBuffer(W83bClsConstantes.UNO)/*.append(idDocumento)*/.append(textDocumento).toString());
				
				contexto.set("w83bListaBorradoFileActas", datosBorrado);
				contexto.set("w83bListaBorradoFileActasCompleto", datosBorradoCompleto);
			}
			else//aņadimos docs
			{
				
				if (contexto.get("w83bMapaFileActas")== null)
				{
					datos=new HashMap();
					contexto.set("w83bMapaFileActas", datos);
				}
				else
				{
					datos=(HashMap)contexto.get("w83bMapaFileActas");
					forward="okVacio";
				}
				if ( ServletFileUpload.isMultipartContent(request))
				{
					HashMap fileData=new HashMap();
					FileItemFactory factory = new DiskFileItemFactory();
					ServletFileUpload upload = new ServletFileUpload(factory);
					List items = upload.parseRequest(request);
					Iterator iter = items.iterator();
					while (iter.hasNext()) {
						FileItem item = (FileItem) iter.next();
						if (item.isFormField()) {
							fileData.put(item.getFieldName(),item.getString());
						} else {
							fileData.put(item.getFieldName(), item.getInputStream());
							fileData.put("tamanyo",new Long(item.getSize()));//NOPMD
							if (item.getSize()==0){
								forward="ficherovacio";
							}
						}
					}
					fileData.put("flag", "A");
					if (!"ficherovacio".equals(forward)){
						datos.put("file_"+(String)fileData.get("indice"), fileData);
					}
					
				}
				
			}
	}
		return mapping.findForward(forward);
		
	}


}
